
import 'package:flutter/material.dart';
import 'package:flutter_changdian_football/api/req/other/req_other.dart';
import 'package:flutter_changdian_football/model/other/protocol_model.dart';
import 'package:flutter_changdian_football/widget/screenutil/custom_screenutil.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';

class CustomHttpWebViewPage extends StatefulWidget {
  const CustomHttpWebViewPage({
    Key? key,
    this.title,
    this.type
  }) : super(key: key);

  final String? title;

  final String? type;

  @override
  State<CustomHttpWebViewPage> createState() => _CustomHttpWebViewPageState();
}

class _CustomHttpWebViewPageState extends State<CustomHttpWebViewPage> {

  double customProgress = 0;

  ProtocolModel? protocolModel;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    initData();
  }

  initData() async {
    var resp = await ReqOther.getProtocol(widget.type ?? '');
    if (resp.data['code'] == '200') {
      setState(() {
        protocolModel = ProtocolModel.fromJson(resp.data['data']);
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomInset: false,
      backgroundColor: const Color(0xffF8FAF9),
      appBar: AppBar(
        backgroundColor: const Color(0xffF8FAF9),
        elevation: 0.0,
        iconTheme: const IconThemeData(
          color: Color(0xff333333)
        ),
        title: Text(widget.title ?? '', style: TextStyle(fontSize: 16.sp, color: const Color(0xff333333), fontWeight: FontWeight.w500),),
        centerTitle: true,
        bottom: customProgress == 1.0 ? PreferredSize(
          preferredSize: Size.fromHeight(0.w),
          child: Container(),
        ) : PreferredSize(
          preferredSize: Size.fromHeight(1.w),
          child: LinearProgressIndicator(
            minHeight: 2.w,
            backgroundColor: Colors.white70.withOpacity(0),
            value: customProgress == 1.0 ? 0 : customProgress,
            valueColor: const AlwaysStoppedAnimation<Color>(Color(0xff499360)),
          ),
        ),
      ),
      body: protocolModel == null ? Center(
        child: CircularProgressIndicator(
          color: const Color(0xff499360),
          strokeWidth: 4.w,
        ),
      ) : InAppWebView(
        initialUrlRequest: URLRequest(url: Uri.parse("${protocolModel?.url}")),
        initialOptions: InAppWebViewGroupOptions(
          android: AndroidInAppWebViewOptions(
            mixedContentMode: AndroidMixedContentMode.MIXED_CONTENT_ALWAYS_ALLOW
          )
        ),
        onProgressChanged: (controller, progress) {
          setState(() {
            customProgress = progress / 100;
          });
        },
      ),
    );
  }
}